package com.duan.config;

import com.duan.anno.PasswordValid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author db
 * @version 1.0
 * @description PasswordValidValidator
 * @since 2024/6/25
 */
public class PasswordValidValidator implements ConstraintValidator<PasswordValid,String> {
    @Override
    public void initialize(PasswordValid constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }

        boolean hasUppercase = value.chars().anyMatch(Character::isUpperCase);
        boolean hasLowercase = value.chars().anyMatch(Character::isLowerCase);
        boolean hasDigit = value.chars().anyMatch(Character::isDigit);

        return value.length() >= 8 && hasUppercase && hasLowercase && hasDigit;
    }
}
